package com.miao.elm.controller;

import com.miao.elm.entity.UserEntity;
import com.miao.elm.service.UserService;
import com.miao.elm.to.UserTo;
import com.miao.elm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("UserController")
public class UserController {
    @Autowired
    private UserService userService;

    //根据用户编号与密码查询用户信息
    @PostMapping("/getUserByIdByPass")
    public UserEntity getUserByIdByPass(String userId, String password){
        return userService.getUserByIdByPass(userId, password);
    }

    //根据用户编号查询用户表返回的行数
    @GetMapping("/getUserById")
    public int getUserById(@RequestParam("userId") String userId){
        return userService.getUserById(userId);
    }

    //保存用户
    @PostMapping("/saveUser")
    public int saveUser(UserTo userTo){
        return userService.saveUser(userTo);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") String userId){
		UserEntity user = userService.getById(userId);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] userIds){
		userService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
